package com.test.demo.dao;

import com.test.demo.model.Book;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

/**
 * 测试缓存，当不开启缓存时，没3秒打印一次，开启缓存瞬间打印
 */
@Component
public class SimPleBookDaoImpl implements BookDao {
    @Override
    @Cacheable("books")      //开启缓存
    public Book getBookByIbsn(String ibsn) {
        simulateSlowService();
        return new Book(ibsn,"Some book");
    }

    /**
     * 暂停3秒，模拟查询数据库时间
     */
    private void simulateSlowService() {
        try {
            long time = 3000L;
            Thread.sleep(time);
        } catch (InterruptedException e) {
            throw new IllegalStateException(e);
        }
    }
}
